package cn.com.maven.designpattern.singleton;

/**
 *
 * 饿汉式单例模式的另一种形式，将new操作放在了静态语句块中，效果等同HungrySingleton
 * @Author YongQiang
 * @Date 2021/5/18 10:03
 * @Version 1.0
 */
public class HungarySingleton2 {
    // static实例化
    private static HungarySingleton2 instance = null;

    static {
        instance = new HungarySingleton2();
    }

    //构造方法设为私有,禁止外部new对象
    private HungarySingleton2(){

    }

    public static HungarySingleton2 getInstance(){
        return instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> System.out.println(HungarySingleton2.getInstance())).start();
        }
    }
}
